﻿namespace EFS.RC.CCAdmin.Customers
{
	public partial class List : EFS.RC.CCAdmin.Common.Control
	{
		#region Constructor

		public List()
		{
			InitializeComponent();

			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//set listener up for list load
				UtilitiesService.CustomerEndPoints_GetAllDetailsCompleted += new System.EventHandler<WebProxy.CustomerEndPoints_GetAllDetailsCompletedEventArgs>(UtilitiesService_CustomerEndPoints_GetAllDetailsCompleted);

				//delete
				UtilitiesService.CustomerEndPoint_DeleteCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_CustomerEndPoint_DeleteCompleted);
			}

			// set events
			this.Loaded += new System.Windows.RoutedEventHandler(List_Loaded);
		}

		#endregion

		#region Service Events

		void UtilitiesService_CustomerEndPoints_GetAllDetailsCompleted(object sender, WebProxy.CustomerEndPoints_GetAllDetailsCompletedEventArgs e)
		{
			// close any notifications
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			// check
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to retrieve Phones.", e.Error);

				// done
				return;
			}

			// check
			if (null == e.Result)
			{
				// done
				return;
			}

			try
			{
				// deserialize
				EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails[] customers =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails[]>(e.Result.ToString());

				//bind/fill form
				dataGridCustomers.ItemsSource = customers;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Unable to load data. " + ex.Message);
			}
		}

		void UtilitiesService_CustomerEndPoint_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to delete Phone.", e.Error);
			}
			else
			{
				LoadPhones();
			}
		}

		#endregion

		#region Content loading

		private void LoadPhones()
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

			// load
			UtilitiesService.CustomerEndPoints_GetAllDetailsAsync();
		}

		#endregion

		#region Events

		private void List_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				LoadPhones();
			}
		}

		private void ToolbarItem_NewCustomerEndPoint_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.Common.UI.Controls.Toolbar.ToolbarItem toolbarItem = (EFS.Common.UI.Controls.Toolbar.ToolbarItem)sender;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Customers.Edit(wnd, null);
			wnd.Show(OnNewWindowClosed);
		}

		//add new skill
		private void OnNewWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason != EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok) return;

			// reload
			LoadPhones();
		}

		//update skill
		private void OnEditWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason != EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok) return;

			// reload
			LoadPhones();
		}

		private void DeleteButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (System.Windows.Browser.HtmlPage.Window.Confirm("Permanently delete phone?"))
			{
				EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails customer = (EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails)dataGridCustomers.SelectedItem;
				UtilitiesService.CustomerEndPoint_DeleteAsync(customer.ID.Value);
			}
		}

		private void EditButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// get sel
			EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails customer = (EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails)dataGridCustomers.SelectedItem;

			// 
			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Customers.Edit(wnd, customer);
			wnd.Show(OnEditWindowClosed);
		}

		#endregion
	}
}